package com.ahyesyesteam.aicarfaultrepair.service.impl;

import com.ahyesyesteam.aicarfaultrepair.mapper.CommentMapper;
import com.ahyesyesteam.aicarfaultrepair.mapper.CommentUserLikeRelationMapper;
import com.ahyesyesteam.aicarfaultrepair.pojo.Comment;
import com.ahyesyesteam.aicarfaultrepair.pojo.CommentUserLikeRelation;
import com.ahyesyesteam.aicarfaultrepair.service.ICommentService;
import com.ahyesyesteam.aicarfaultrepair.util.Response;
import com.ahyesyesteam.aicarfaultrepair.util.UserThreadLocal;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SSQ
 * @since 2022-06-17
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    CommentUserLikeRelationMapper commentUserLikeRelationMapper;

    @Override
    public Comment addComment(Comment comment) {
        comment.setCreateDate(LocalDateTime.now());
        comment.setLikeCount(0);
        comment.setUserId(UserThreadLocal.get().getId());
        comment.setUserNickname(UserThreadLocal.get().getNickname());
        this.getBaseMapper().insert(comment);
        return comment;
    }

    @Override
    public boolean deleteComment(Long commentId) {
        Comment byId = this.getById(commentId);
        if(byId==null) return true;
        if(byId.getUserId().equals(UserThreadLocal.get().getId())) {
            return this.removeById(commentId);
        }else{
            return false;
        }
    }

    @Override
    @Transactional
    public Response likeComment(Long commentId) {
        Comment byId = this.getById(commentId);
        Long userId = UserThreadLocal.get().getId();

        QueryWrapper<CommentUserLikeRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId)
                .eq("user_id", userId);
        CommentUserLikeRelation commentUserLikeRelation = commentUserLikeRelationMapper.selectOne(queryWrapper);
        boolean res;
        if(commentUserLikeRelation!=null){
            byId.setLikeCount(byId.getLikeCount()-1);
            commentUserLikeRelationMapper.delete(queryWrapper);
            res = false;
        }else{
            byId.setLikeCount(byId.getLikeCount()+1);
            CommentUserLikeRelation newCommentUserLikeRelation = new CommentUserLikeRelation();
            newCommentUserLikeRelation.setCommentId(commentId);
            newCommentUserLikeRelation.setUserId(userId);
            commentUserLikeRelationMapper.insert(newCommentUserLikeRelation);
            res = true;
        }


        return Response.ok().put("data",res);
    }


    @Override
    public List<Comment> getCommentList(Long solutionId) {
        List<Comment> commentList = this.getBaseMapper().selectList(new QueryWrapper<Comment>().eq("solution_id", solutionId));
        return commentList;
    }

    @Override
    public boolean isLike(Long commentId) {
        Long userId = UserThreadLocal.get().getId();

        QueryWrapper<CommentUserLikeRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId)
                .eq("user_id", userId);
        CommentUserLikeRelation commentUserLikeRelation = commentUserLikeRelationMapper.selectOne(queryWrapper);
        return commentUserLikeRelation!=null;
    }
}
